package com.gengao.springbootcase.controller;

import com.gengao.springbootcase.pojo.Result;
import com.gengao.springbootcase.utils.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.security.interfaces.DSAPrivateKey;

@RestController
public class UploadController {

    private static final Logger log = LoggerFactory.getLogger(UploadController.class);

    @Value("${file.upload-dir}")
    private String uploadDir;
    @Autowired
    private AliOSSUtils aliOSSUtils;
//本地存储
//    @PostMapping("/upload")
//    public ResponseEntity<Result> upload(@RequestParam("image") MultipartFile file) {
//        if (file.isEmpty()) {
//            return ResponseEntity.badRequest().body(Result.error("文件为空"));
//        }
//        try {
//            String originalFilename = file.getOriginalFilename();
//            String newFileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//            String newPath = uploadDir + newFileName;
//            file.transferTo(new File(newPath)); // 保存到本地
//            String returnPath = "http://localhost:8080/static/" + newFileName;
//            log.debug("File uploaded to [{}]", newPath);
//            return ResponseEntity.ok(Result.success(returnPath));
//        } catch (IOException e) {
//            log.error("File upload failed", e);
//            return ResponseEntity.internalServerError().body(Result.error("文件上传失败"));
//        }
//    }
    @PostMapping("/upload")
    public Result upload(@RequestParam("image") MultipartFile file) throws Exception {
        String url=aliOSSUtils.uploadFile(file);
        return Result.success(url);
    }
}
